#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# nodeserver.py: node manager server
#
# Copyright (C) 2008-2010 Hily Jiang <hilyjiang@gmail.com>
#
# This file is part of dtask.
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
#    claim that you wrote the original software. If you use this software
#    in a product, an acknowledgment in the product documentation would be
#    appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

"""
Node Manager Server.

It include two interfaces.
One for Node Client, another for Scheduler.
"""

import threading
import socket
from time import sleep

from node.nodmgr import NodeManager
from node.dispatcher import Dispatcher
from node.nodif import *
from node.schif import *


CFG_SCH_IP = '127.0.0.1'
CFG_SCH_PORT = 8809
CFG_NOD_IP = ''
CFG_NOD_PORT = 8810

CFG_NODE_SQLITEDB = ':memory:'
CFG_DPC_SQLITEDB = ':memory:'


def main():
    try:
        nodServerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        nodServerSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        nodServerSock.bind((CFG_NOD_IP, CFG_NOD_PORT))
        nodServerSock.listen(0)
        print 'Node interface listen on: %s:%s' % (CFG_NOD_IP, CFG_NOD_PORT)

        schServerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        schServerSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        schServerSock.bind((CFG_SCH_IP, CFG_SCH_PORT))
        schServerSock.listen(0)
        print 'Scheduler interface listen on: %s:%s' % (CFG_SCH_IP, CFG_SCH_PORT)
    except Exception, e:
        print e

    nodClientSocks = []
    schClientSocks = []
    nodSockDict = {}
    schSockDict = {}

    nodeManager = NodeManager(CFG_NODE_SQLITEDB, jobsPerNode=2)
    dispatcher = Dispatcher(CFG_DPC_SQLITEDB, clearInterval=600, dispatchTimeout=300)

    nodThread = threading.Thread(target=startNodeIf,
                 args=(nodeManager, dispatcher, nodServerSock, nodClientSocks,
                       nodSockDict, schSockDict))
    schThread = threading.Thread(target=startSchedulerIf,
                 args=(nodeManager, dispatcher, schServerSock, schClientSocks,
                       nodSockDict, schSockDict))
    nodThread.setDaemon(True)
    schThread.setDaemon(True)
    nodThread.start()
    schThread.start()

    try:
        while True: sleep(100)
    except KeyboardInterrupt:
        print 'Application interface quit by KeyboardInterrupt'
        nodServerSock.close()
        for sock in nodClientSocks:
            sock.close()
        print 'Scheduler interface quit by KeyboardInterrupt'
        schServerSock.close()
        for sock in schClientSocks:
            sock.close()


if __name__ == '__main__':
    main()
